package com.group02.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;


import com.group02.community.entity.community.ZyBuilding;
import com.group02.community.entity.community.ZyUnit;
import com.group02.community.service.BuildingService;
import com.group02.community.utils.EasyExcelUtil;
import com.group02.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;


@RestController
@CrossOrigin
@Api
@RequestMapping("/building")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    /**
     * 分页查询列表
     *
     * @param currentPage
     * @param pageSize
     * @param zyBuilding
     * @return
     */
    @ApiOperation("分页查询楼栋列表")
    @PostMapping("/page")
    public ResponseResult<IPage<ZyBuilding>> getBuildingPage(Integer currentPage, Integer pageSize, @RequestBody ZyBuilding zyBuilding) {
        IPage<ZyBuilding> buildingPage = buildingService.getBuildingPage(currentPage, pageSize, zyBuilding);

        return ResponseResult.ok(buildingPage);

    }



    /**
     * 添加楼栋
     *
     * @param zyBuilding
     * @return
     */
    @ApiOperation("添加楼栋")
    @PostMapping("/add")
    public ResponseResult<String> addBuilding(@RequestBody ZyBuilding zyBuilding) {

        zyBuilding.setCommunityId("1338423795842494466");
        System.out.println(zyBuilding);
        buildingService.save(zyBuilding);
        return ResponseResult.ok("添加成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation("根据数组批量删除楼栋")
    @PostMapping("/deleteByIds")
    public ResponseResult<String> deleteCommunityByIds(@RequestBody String[] ids) {
        System.out.println(ids);
        buildingService.removeBatchByIds(Arrays.asList(ids));
        return ResponseResult.ok("删除成功");
    }

    /**
     * 导出楼栋数据
     *
     * @param ids
     * @param response
     * @return
     */
    @ApiOperation("导出楼栋数据")
    @PostMapping("/excel")
    public ResponseResult<String> exportCommunityByIds(@RequestBody String[] ids, HttpServletResponse response) {
        ArrayList<String> list = new ArrayList<>(Arrays.asList(ids));
        List<ZyBuilding> zyBuildings = buildingService.listByIds(list);
        String fileName = UUID.randomUUID().toString();
        String path = "C:\\Users\\Administrator\\Desktop\\" + fileName + "楼栋信息表.xls";    //导出路径
        EasyExcelUtil.write(path, ZyBuilding.class, zyBuildings, 0, "楼栋信息表");
        return ResponseResult.ok("导出成功");
    }

    @ApiOperation("修改楼栋")
    @PutMapping("/update")
    public ResponseResult<String> update(@RequestBody ZyBuilding zyBuilding) {
        buildingService.updateById(zyBuilding);
        System.out.println(zyBuilding);
        return ResponseResult.ok("修改成功");
    }

    @ApiOperation("获取所有的楼栋信息")
    @GetMapping("/all")
    public ResponseResult<List<ZyBuilding>> getList() {
        return ResponseResult.ok(buildingService.list(null));
    }


    @ApiOperation("根据楼栋id获取楼栋下的单元")
    @GetMapping("/unitList/{buildingId}")
    public ResponseResult<List<ZyUnit>> getUnitListByBuildingId(@PathVariable  String buildingId){
        return  ResponseResult.ok(buildingService.getUnitListByBuildingId(buildingId));
    }

}
